package reviewAlgorithms.dynamicProgramming;

public class MaxProfit {
    public static void main(String[] args) {
        int[] prices = {1,2,4,7};
        System.out.println(maxProfit(prices));
    }
    public static int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2){
            return 0;
        }
        int[] dp = new int[prices.length];
        dp[0] = 0;
        for (int i = 1; i < prices.length; i++) {
            int maxk = 0;
            int maxj = 0;
            if (i >= 3){
                for (int j = 3; j <= i + 1 ; j++) {
                    if (i > j){
                        for (int k = 2; k < j; k++) {
                            int temp = dp[i - j] + prices[i] - prices[i - j + k];
                            maxk = Math.max(maxk,temp);
                        }
                    }else {
                        for (int k = 1; k < j; k++) {
                            int temp = prices[i] - prices[i - j + k];
                            maxk = Math.max(maxk,temp);
                        }
                    }
                    maxj = Math.max(maxj,maxk);
                }
            }else {
                for (int k = 0; k < i; k++) {
                    maxk = Math.max(maxk,prices[i] - prices[k]);
                }
                maxj = Math.max(maxj,maxk);
            }
            dp[i] = Math.max(dp[i - 1],maxj);
        }
        return dp[prices.length - 1];
    }
}
